package com.ryan.demo.jms.tibco;

import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.junit.Test;

import com.ryan.demo.io.utils.ReadFromFile;
import com.ryan.demo.path.utils.PathDemo;


public class SimpletJms {
	
	private static int mqGroup=2;
	
	private static String SEND_MQ;
	
	private static String RECEIVE_MQ;

	private static String SOURCE_ID1;
	private static String SOURCE_ID2;
	
	private static String MQ_IP;
	
	static{
		initMqInfo();
	}
	
	private static void initMqInfo(){
		if(mqGroup == 1){//local
			SEND_MQ = "notifyTestCommand";
			RECEIVE_MQ = "notifyTestCommand";
			SOURCE_ID1 = "test1";
			SOURCE_ID2 = "test2";
			MQ_IP = "tcp://192.168.164.179:7222";
		}else if(mqGroup == 2){//dev
			SEND_MQ = "notifyTestCommand";
			RECEIVE_MQ = "notifyTestCommand";
			SOURCE_ID1 = "test1";
			SOURCE_ID2 = "test2";
			MQ_IP = "tcp://192.168.63.8:7222";
		}else if(mqGroup == 3){//dev
			SEND_MQ = "notify.demo.RequestQueue";
			RECEIVE_MQ = "notify.demo.RequestQueue";
//			RECEIVE_MQ = "notify.demo.ResponseQueue";
			SOURCE_ID1 = "test1";
			SOURCE_ID2 = "test2";
			MQ_IP = "tcp://192.168.1.6:7222";
		}
	}
	

	@Test
	public void sendMessageTest() {
		String seadMessage = "你好";
		this.sendMessage(seadMessage);
	}
	

	@Test
	public void sendCpProductOpenTest(){
		String classPath = PathDemo.getClassPathByClass(this.getClass());
		String request = ReadFromFile.fileToString(classPath+"/testData/20120502/crmToOas/CP/CPMessage.XML");
		this.sendMessage(request);
	}
	

	@Test
	public void sendCnpProductOpenTest(){
		String classPath = PathDemo.getClassPathByClass(this.getClass());
		String request = ReadFromFile.fileToString(classPath+"/testData/20120502/crmToOas/CNP/CNP.xml");
		this.sendMessage(request);
	}
	
	
	@Test
	public void sendMessageByCorrelationIdTest() {
		this.initMqInfo();
		String seadMessage = "你好";
		this.sendMessageByCorrelationId(seadMessage,SOURCE_ID1);
	}
	
	@Test
	public void receveAllMessageTest(){
		this.initMqInfo();
		this.receveAllMessage();
	}
	
	@Test
	public void receveMessageByCorrelationIdTest(){
		this.initMqInfo();
		this.receveMessageByCorrelationId(this.SOURCE_ID1);
	}
	
	public void sendMessage(String seadMessage) {
		try {
			System.out.println("before send message!");	
			Properties pops = new Properties();
			pops.setProperty("java.naming.factory.initial", "com.tibco.tibjms.naming.TibjmsInitialContextFactory");
			pops.setProperty("java.naming.provider.url", MQ_IP);
			Context ctx = new InitialContext(pops);
//			ConnectionFactory cf = (ConnectionFactory) ctx.lookup("mdsNonXASendConnectionFactory");
			ConnectionFactory cf = (ConnectionFactory) ctx.lookup("GenericConnectionFactory");
			Connection conn = cf.createConnection("admin", "admin");
			Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
			Destination dest = (Destination) ctx.lookup(SEND_MQ);
			MessageProducer producer = session.createProducer(dest);
			TextMessage message = session.createTextMessage();
			message.setText(seadMessage);
			System.err.println("send message is:《"+seadMessage+"》");
			producer.send(message);
			System.out.println("after message end!");	
			session.close();
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
	
	public void sendMessageByCorrelationId(String seadMessage,String correlationId) {
		try {
			System.out.println("before send message!");	
			Properties pops = new Properties();
			pops.setProperty("java.naming.factory.initial", "com.tibco.tibjms.naming.TibjmsInitialContextFactory");
			pops.setProperty("java.naming.provider.url", MQ_IP);
			Context ctx = new InitialContext(pops);
			ConnectionFactory cf = (ConnectionFactory) ctx.lookup("GenericConnectionFactory");
			Connection conn = cf.createConnection("admin", "admin");
			Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
			Destination dest = (Destination) ctx.lookup(SEND_MQ);
			MessageProducer producer = session.createProducer(dest);
			TextMessage message = session.createTextMessage();
//			设置JMSCorrelationID
			message.setJMSCorrelationID(correlationId);
			message.setText(seadMessage);
			System.err.println("send message is:《"+seadMessage+"》,correlationId is:《"+correlationId+"》");
			producer.send(message);
			System.out.println("after message end!");	
			session.close();
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
	
	
	public void receveAllMessage(){
		try {
			System.out.println("before receving message");
			Properties pops = new Properties();
			pops.setProperty("java.naming.factory.initial", "com.tibco.tibjms.naming.TibjmsInitialContextFactory");
			pops.setProperty("java.naming.provider.url", MQ_IP);
			Context ctx = new InitialContext(pops);
			ConnectionFactory cf = (ConnectionFactory) ctx.lookup("GenericConnectionFactory");
			Connection conn = cf.createConnection("admin", "admin");
			Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
			Destination dest = (Destination) ctx.lookup(RECEIVE_MQ);
			MessageConsumer consumer = session.createConsumer(dest);			
			conn.start();
			TextMessage message = null;
			while((message = (TextMessage) consumer.receiveNoWait()) != null){
				System.err.println("received message is:《"+message.getText()+"》");
			}
			System.out.println("after receving message");
			conn.close();
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
	
	public void receveMessageByCorrelationId(String correlationId){
		try {
			System.out.println("before receving message");
			Properties pops = new Properties();
			pops.setProperty("java.naming.factory.initial", "com.tibco.tibjms.naming.TibjmsInitialContextFactory");
			pops.setProperty("java.naming.provider.url", MQ_IP);
			Context ctx = new InitialContext(pops);
			ConnectionFactory cf = (ConnectionFactory) ctx.lookup("GenericConnectionFactory");
			Connection conn = cf.createConnection("admin", "admin");
			Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
			Destination dest = (Destination) ctx.lookup(RECEIVE_MQ);
			MessageConsumer consumer = session.createConsumer(dest, "JMSCorrelationID='"+correlationId+"'");	
			conn.start();
			TextMessage message = null;
			while((message = (TextMessage) consumer.receiveNoWait()) != null){
				System.err.println("received message is:《"+message.getText()+"》,correlationId is:《"+correlationId+"》");
			}
			System.out.println("after receving message");
			conn.close();
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
}
